#include <string>
#include <stdio.h>
#include "hi_mapi_disp_define.h"
#include "hi_mapi_disp.h"
#include "hi_mapi_vproc.h"
#include "hi_mapi_hdmi.h"
#include "hi_mapi_sys.h"
#include "hi_mapi_osd.h"
#include "HiDisp.h"

static HI_VOID HI_HDMI_EventPro(HI_HDMI_EVENT_TYPE_E event, HI_VOID* pPrivateData)
{
    switch (event)
    {
        case HI_HDMI_EVENT_HOTPLUG:
            printf("\033[0;32mDisplay HDMI HOTPLUG event! \033[0;39m\n");
            break;

        case HI_HDMI_EVENT_NO_PLUG:
            printf("\033[0;31mDisplay HDMI NO_PLUG event! \033[0;39m\n");
            break;

        default :
            printf("\033[0;31minvalid Display HDMI event! \033[0;39m\n");
            break;
    }
}

HiDisp::HiDisp()
{
    m_vpssHdl = -1;
    m_vpssHdl = -1;
    m_dispHdl = -1;
    m_wndHdl = 0; // use default 0
}

HiDisp::~HiDisp()
{
}



HI_S32 HiDisp::Open(HI_HANDLE dispHdl, HI_S32 width, HI_S32 height)
{

     HI_MAPI_DISP_ATTR_S stDispAttr = {0};
     HI_MAPI_DISP_VIDEOLAYER_ATTR_S stVideoLayerAttr = {0};

     stDispAttr.stPubAttr.u32BgColor = 0xFF;
     stDispAttr.stPubAttr.enIntfType = VO_INTF_HDMI;
     if(width == 1920 && height == 1080) {
        stDispAttr.stPubAttr.enIntfSync = VO_OUTPUT_1080P30;
     } else {
         printf("not support this resolution now, you should adapt it \n");
         return  -1;
     }

     memset(&stDispAttr.stPubAttr.stSyncInfo, 0, sizeof(VO_SYNC_INFO_S));
     memset(&stVideoLayerAttr, 0, sizeof(HI_MAPI_DISP_VIDEOLAYER_ATTR_S));
     stVideoLayerAttr.u32BufLen = 0;

     HI_MAPI_DISP_Init(dispHdl, &stDispAttr);
     HI_MAPI_DISP_Start(dispHdl, &stVideoLayerAttr);

     /* starts HDMI */
     HI_MAPI_HDMI_ATTR_S sthdmiattr;

     memset(&sthdmiattr, 0, sizeof(HI_MAPI_HDMI_ATTR_S));
     sthdmiattr.bEnableHdmi = HI_TRUE;
     sthdmiattr.bEnableVideo = HI_TRUE;
     sthdmiattr.enHdmiSync = VO_OUTPUT_1080P30;
     sthdmiattr.bEnableAudio = HI_FALSE;
     sthdmiattr.enSampleRate = AUDIO_SAMPLE_RATE_48000;

     HI_HDMI_CALLBACK_FUNC_S stHdmiCallBack;
     stHdmiCallBack.pfnHdmiEventCallback = (HI_HDMI_CallBack)HI_HDMI_EventPro;
     stHdmiCallBack.pPrivateData = HI_NULL;

     HI_MAPI_HDMI_Init(HI_HDMI_ID_0, &stHdmiCallBack);
     HI_MAPI_HDMI_SetAttr(HI_HDMI_ID_0, &sthdmiattr);
     HI_MAPI_HDMI_Start(HI_HDMI_ID_0);

     /* start window */
     HI_MAPI_DISP_WINDOW_ATTR_S stWndAttr;
     stWndAttr.stRect.s32X = 0;
     stWndAttr.stRect.s32Y = 0;
     stWndAttr.stRect.u32Width = width;
     stWndAttr.stRect.u32Height = height;
     stWndAttr.u32Priority = 0;
     HI_MAPI_DISP_SetWindowAttr(dispHdl, m_wndHdl, &stWndAttr);
     HI_MAPI_DISP_StartWindow(dispHdl, m_wndHdl);
     m_dispHdl = dispHdl;
     return 0;
}


HI_S32 HiDisp::Close()
{
     /* stop preview */
     if(m_vpssHdl != (HI_HANDLE)-1) {
        HI_MAPI_DISP_UnBind_VProc(m_vpssHdl, m_vportHdl, m_dispHdl, m_wndHdl, HI_FALSE);
     }

     if (m_dispHdl != (HI_HANDLE)-1) {
         HI_MAPI_DISP_StopWindow(m_dispHdl, m_wndHdl);
         HI_MAPI_HDMI_Stop(HI_HDMI_ID_0);
         HI_MAPI_HDMI_Deinit(HI_HDMI_ID_0);
         HI_MAPI_DISP_Stop(m_dispHdl);
         HI_MAPI_DISP_Deinit(m_dispHdl);
     }

     return 0;
}


HI_S32 HiDisp::BindVProc(HI_HANDLE vpssHdl, HI_HANDLE vportHdl)
{
    m_vpssHdl = vpssHdl;
    m_vportHdl = vportHdl;
    return HI_MAPI_DISP_Bind_VProc(vpssHdl, vportHdl, m_dispHdl, m_wndHdl, HI_FALSE);
}


